home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / src / stab.macho.c < prev    next >
C/C++ Source or Header  |  1992-10-23  |  3KB  |  79 lines

  1. #include <nlist.h>
  2. #include <mach-o/rld.h>
  3.  
  4. static SYMTAB *Grovel_Over_Nlist (symcmd, nl, strtab, text_sect)
  5.     struct symtab_command *symcmd; /* ptr to MACH-O symtab command */
  6.     struct nlist nl[];             /* ptr to symbol table */
  7.     char *strtab;                  /* ptr to string table */
  8.     long text_sect;                /* # of text section */ {
  9.  
  10.     SYMTAB *tab;
  11.     register SYM *sp, **nextp;
  12.     long i;
  13.  
  14.     tab = (SYMTAB *) Safe_Malloc (sizeof (SYMTAB));
  15.     tab->first = 0;
  16.     tab->strings = 0;
  17.     nextp = &tab->first;
  18.  
  19.     /* Grovel over the file's nlist, extracting global symbols that
  20.      * have a section mumber equal to the number of the text section:
  21.      */
  22.     for (i = 0; i < symcmd->nsyms; i++) {
  23.     if ((nl[i].n_type & (N_TYPE|N_EXT)) == (N_SECT|N_EXT) &&
  24.         nl[i].n_sect == text_sect) {
  25.         sp = (SYM *)Safe_Malloc (sizeof (SYM));
  26.         sp->name = strtab + nl[i].n_un.n_strx;
  27.         sp->value = nl[i].n_value;
  28.         sp->next = 0;
  29.         *nextp = sp;
  30.         nextp = &sp->next;
  31.     }
  32.     }
  33.     return tab;
  34. }
  35.  
  36. SYMTAB *Snarf_Symbols (mhdr) struct mach_header *mhdr; {
  37.     struct load_command *ld_cmd;
  38.     struct symtab_command *sym_cmd;
  39.     struct segment_command *seg_cmd;
  40.     struct section *sp;
  41.     struct nlist *symtab = 0;
  42.     char *cmdptr, *strtab;
  43.     long i, j, text_sect = 0;
  44.  
  45.     /* Loop through the load commands, find the symbol table and
  46.      * the segment command carrying the text section to determine
  47.      * the number of the text section:
  48.      */
  49.     cmdptr = (char *)mhdr + sizeof (struct mach_header);
  50.     for (i = 0; i < mhdr->ncmds; i++) {
  51.     ld_cmd = (struct load_command *)cmdptr;
  52.     if (ld_cmd->cmd == LC_SYMTAB && !symtab) {
  53.         sym_cmd = (struct symtab_command *)ld_cmd;
  54.         symtab = (struct nlist *)((char *)mhdr + sym_cmd->symoff);
  55.         strtab = (char *)mhdr + sym_cmd->stroff;
  56.     } else if (ld_cmd->cmd == LC_SEGMENT && !text_sect) {
  57.         seg_cmd = (struct segment_command *)ld_cmd;
  58.         sp = (struct section *)
  59.         ((char *)ld_cmd + sizeof (struct segment_command));
  60.         for (j = 1; j <= seg_cmd->nsects && !text_sect; j++, sp++)
  61.         if (strcmp (sp->sectname, SECT_TEXT) == 0)
  62.             text_sect = j;
  63.     }
  64.     cmdptr += ld_cmd->cmdsize;
  65.     }
  66.     if (!symtab)
  67.     Primitive_Error ("couldn't find symbol table in object file");
  68.     if (!text_sect)
  69.     Primitive_Error ("couldn't find text section in object file");
  70.     return Grovel_Over_Nlist (sym_cmd, symtab, strtab, text_sect);
  71. }
  72.  
  73. #ifdef INIT_OBJECTS
  74. SYMTAB *Open_File_And_Snarf_Symbols (name) char *name; {
  75.     extern char *_mh_execute_header;
  76.     return Snarf_Symbols ((struct mach_header *)&_mh_execute_header);
  77. #endif /* INIT_OBJECTS */
  78.